let {Post,Ajax} =require('./util/HttpHelper');
let qs=require('querystring');

let user={
    phone:'15939949045',
    pwd:'zlj90921',
    code:'285274',
    cookie:null,
    seckey:null,
    info:null,
};
function AiGet(url,callback){
    Ajax({
        url,
        headers:{
            Cookie:user.cookie
        }
    },(err,res,body)=>{
        if(err){
            console.log(err.message);
        }else{
            callback && callback(res,body);
        }
    })
}
function AiPost(url,data,callback){
    let par=qs.stringify(data);
    Ajax({
        url,
        method:'POST',
        form:par,
        headers:{
            Cookie:user.cookie
        }
    },(err,res,body)=>{
        if(err){
            console.log(err.message);
        }else{
            callback && callback(res,JSON.parse(body));
        }
    })
}
function Login(callback){
    Post('https://daypc.vzsite.top/login.php',{
        phone:user.phone,
        passwd:user.pwd,
        code:user.code,
        dataType:'json',
        ccode:'+86',
        type:1
    },function(err,res,body){
        if(err){
            console.log('登陆时出错！！');
        }else{
            let json=JSON.parse(body);
            if(json.state){
                console.log(json.msg);
            }else{
                user.cookie=this.headers['set-cookie'];
                console.log('登陆成功！！！');
                callback && callback.call(res,json);
            }
        }
    })
}
function OutLogin(){
    Ajax({
        url:'https://daypc.vzsite.top/logout.php',
        headers:{
            Cookie:user.cookie
        }
    },(err,res,body)=>{
        if(err){
            console.log(err.message);
        }else{
            console.log('退出登陆成功！！');
        }
    })
}
function GetSeckey(callback){
    AiGet('https://daypc.vzsite.top/index.php',(res,body)=>{
        let key=body.match(/(?<=(var seckey = \"))[.\s\S]*?(?=(\";))/g)[0];
        user.seckey=key;
        callback && callback(key);
    });
}
function GetSocket(callback){
    AiPost('https://daypc.vzsite.top/index.php',{
        dataType: 'json',
        seckey:user.seckey,
        type:1
    },(res,json)=>{
        console.log(json);
        if(json.state===0){
            if(json.url){
                user.seckey=json.seckey;
                //获取链接
                callback && callback(json.url);
            }
        }else{
            console.log(json);
        }
    })
}
function CookieXt(callback){
    try{
        AiPost('https://daypc.vzsite.top/index.php',{
            dataType: 'json',
            seckey:user.seckey,
            type:2
        },(res,json)=>{
            user.seckey=json.seckey;
            user.info=json.info;
        })
    }catch (e) {
        console.log('登陆失效了！！！');
        callback && callback();
    }
}
(function main(){
   /* Login(()=>{
        console.log(user.cookie);
        GetSeckey(()=>{
            GetSocket(e=>{
                console.log(e);
                console.log('-----------');
                console.log(user.cookie);
            })
        })
    });*/
})();